package nxu.it.movieapi.service.impl;

import nxu.it.api.model.User;
import nxu.it.movieapi.service.UserRegisterService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserRegisterServiceImpl implements UserRegisterService {


    private List<User> userRepository = new ArrayList<>();

    @Override
    public User register(User user) {
        // 检查用户名和邮箱是否已存在
        if (checkUsernameExists(user.getUsername())) {
            throw new IllegalArgumentException("Username already exists");
        }
        if (checkEmailExists(user.getEmail())) {
            throw new IllegalArgumentException("Email already registered");
        }
        // 注册用户，即添加到模拟的用户存储中
        userRepository.add(user);
        return user;
    }

    @Override
    public boolean checkUsernameExists(String username) {
        // 检查用户名是否已存在
        return userRepository.stream()
                .anyMatch(user -> user.getUsername().equals(username));
    }

    @Override
    public boolean checkEmailExists(String email) {
        // 检查邮箱是否已注册
        return userRepository.stream()
                .anyMatch(user -> user.getEmail().equals(email));
    }
}
